home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / IEditor / IEX.c < prev    next >
C/C++ Source or Header  |  1997-06-17  |  13KB  |  598 lines

  1. /// Include
  2. #include <stdarg.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7.  
  8. #define INTUI_V36_NAMES_ONLY
  9. #define ASL_V38_NAMES_ONLY
  10. #define CATCOMP_NUMBERS
  11.  
  12. #include <exec/nodes.h>                 // exec
  13. #include <exec/lists.h>
  14. #include <exec/memory.h>
  15. #include <exec/ports.h>
  16. #include <exec/libraries.h>
  17. #include <dos/dos.h>                    // dos
  18. #include <libraries/reqtools.h>
  19. #include <clib/exec_protos.h>           // protos
  20. #include <clib/dos_protos.h>
  21. #include <clib/reqtools_protos.h>
  22. #include <clib/intuition_protos.h>
  23. #include <clib/gadtools_protos.h>
  24. #include <pragmas/exec_pragmas.h>       // pragmas
  25. #include <pragmas/dos_pragmas.h>
  26. #include <pragmas/reqtools_pragmas.h>
  27. #include <pragmas/intuition_pragmas.h>
  28. #include <pragmas/gadtools_pragmas.h>
  29.  
  30.  
  31. #include "DEV_IE:defs.h"
  32. #include "DEV_IE:GUI.h"
  33. #include "DEV_IE:GUI_locale.h"
  34. #include "DEV_IE:Include/expanders-protos.h"
  35. #include "DEV_IE:Include/expanders.h"
  36. #include "DEV_IE:Include/expander_pragmas.h"
  37. ///
  38. /// Prototypes
  39. static void     IEXS_Globals( __A0 struct GenFiles * );
  40. static void     IEXS_Setup( __A0 struct GenFiles * );
  41. static void     IEXS_CloseDown( __A0 struct GenFiles * );
  42. static void     IEXS_Headers( __A0 struct GenFiles * );
  43. static void     IEXS_RenderPlusZero( __A0 struct GenFiles * );
  44. static void     IEXS_RenderMinusZero( __A0 struct GenFiles * );
  45. static ULONG    IEXS_IDCMP( __D0 ULONG );
  46. static void     IEXS_Data( __A0 struct GenFiles * );
  47. static void     IEXS_ChipData( __A0 struct GenFiles * );
  48. static void     IEXS_Support( __A0 struct GenFiles * );
  49. static void     IEXS_OpenWnd( __A0 struct GenFiles * );
  50. static void     IEXS_CloseWnd( __A0 struct GenFiles * );
  51. ///
  52. /// Data
  53. struct IEXSrcFun IEXSrcFunctions = {
  54.     IEXS_Globals,
  55.     IEXS_Setup,
  56.     IEXS_CloseDown,
  57.     IEXS_Headers,
  58.     IEXS_RenderMinusZero,
  59.     IEXS_RenderPlusZero,
  60.     IEXS_IDCMP,
  61.     IEXS_Data,
  62.     IEXS_ChipData,
  63.     IEXS_Support,
  64.     IEXS_OpenWnd,
  65.     IEXS_CloseWnd
  66. };
  67.  
  68. static ULONG    ExId = MIN_IEX_ID, FirstExId;
  69. ///
  70.  
  71. //      Expanders
  72. /// GetExpanders
  73. void GetExpanders( void )
  74. {
  75.     struct AnchorPath  *anchorpath;
  76.     UBYTE               buffer[255];
  77.     ULONG               error, ret; 
  78.  
  79.     if( anchorpath = (struct AnchorPath *)AllocMem( sizeof( struct AnchorPath ), MEMF_CLEAR )) {
  80.  
  81.     error = MatchFirst( "PROGDIR:Expanders/#?.iex", anchorpath );
  82.     while( error == 0 ) {
  83.         struct Expander *IEXBase;
  84.  
  85.         strcpy( buffer, "PROGDIR:Expanders/" );
  86.         strcat( buffer, anchorpath->ap_Info.fib_FileName );
  87.  
  88.         if( IEXBase = OpenLibrary( buffer, 37 )) {
  89.  
  90.         FirstExId = ExId;
  91.  
  92.         if(!( ret = IEX_Mount( &IE ))) {
  93.  
  94.             AddGadgetKind( IEXBase, &IEXBase->Node );
  95.  
  96.         } else {
  97.  
  98.             ULONG tags[] = { RT_ReqPos, REQPOS_CENTERSCR,
  99.                      RT_Underscore, '_',
  100.                      RT_Screen, Scr, TAG_DONE };
  101.  
  102.             rtEZRequest( "%s:\nMissing desc file!",
  103.                  "_Ok", NULL, ( struct TagItem * )tags,
  104.                  anchorpath->ap_Info.fib_FileName );
  105.  
  106.             CloseLibrary(( struct Library * )IEXBase );
  107.         }
  108.         }
  109.  
  110.         if (!( error ))
  111.         error = MatchNext( anchorpath );
  112.     }
  113.  
  114.     MatchEnd( anchorpath );
  115.     FreeMem( anchorpath, sizeof( struct AnchorPath ));
  116.     }
  117. }
  118. ///
  119. /// FreeExpanders
  120. void FreeExpanders( void )
  121. {
  122.     struct IEXNode *ex;
  123.  
  124.     while( ex = RemTail(( struct List * )&IE.Expanders )) {
  125.     CloseLibrary(( struct Library * )ex->Base );
  126.     FreeMem( ex, sizeof( struct IEXNode ));
  127.     }
  128. }
  129. ///
  130. /// FreeARexxCmds
  131. void FreeARexxCmds( void )
  132. {
  133.     struct CmdNode *Node;
  134.  
  135.     while( Node = RemTail(( struct List * )&RexxCommands ))
  136.     if( Node->Node.ln_Type == 1 )
  137.         FreeMem( Node, sizeof( struct ExCmdNode ));
  138.     else
  139.         break;
  140. }
  141. ///
  142.  
  143. //      Expander support routines
  144. /// SplitLines
  145. void SplitLines( __A0 UBYTE *Buffer )
  146. {
  147.     UBYTE  *End, *Start;
  148.     BOOL    ok = TRUE;
  149.  
  150.     Start = Buffer;
  151.  
  152.     do {
  153.     while( *Buffer++ != '#' );
  154.     if( *Buffer++ == '#' ) {
  155.         if(!( strncmp( "end", Buffer, 3 ))) {
  156.         End = Buffer;
  157.         ok = FALSE;
  158.         }
  159.     }
  160.     } while( ok );
  161.  
  162.     ok = FALSE;
  163.  
  164.     while( Start < End ) {
  165.  
  166.     if(( Start[0] == '#' ) && ( Start[1] == '#' ))
  167.         ok = TRUE;
  168.  
  169.     if( *Start == '\n' ) {
  170.         if( ok || (( Start[1] == '#' ) && ( Start[2] == '#' ))) {
  171.         *Start = '\0';
  172.         ok = FALSE;
  173.         }
  174.     }
  175.  
  176.     Start++;
  177.     }
  178. }
  179. ///
  180. /// GetFirstLine
  181. UBYTE *GetFirstLine( __A0 UBYTE *Buffer, __A1 STRPTR ID )
  182. {
  183.     for(;;) {
  184.  
  185.     while( *Buffer++ != '#' );
  186.  
  187.     if( *Buffer++ == '#' ) {
  188.  
  189.         if(!( strncmp( "end", Buffer, 3 )))
  190.         return( NULL );
  191.  
  192.         if(!( strcmp( ID, Buffer )))
  193.         return( Buffer + strlen( ID ) + 1 );
  194.     }
  195.     }
  196. }
  197. ///
  198. /// WriteFormatted
  199. void WriteFormatted( __D0 BPTR File, __A0 STRPTR String, __A1 struct Descriptor *Desc )
  200. {
  201.     UWORD       size = 0;
  202.     UBYTE      *ptr, ch;
  203.  
  204.     ptr = String;
  205.  
  206.     while( *String ) {
  207.  
  208.     ch = *String++;
  209.  
  210.     if( ch == '%' ) {
  211.  
  212.         FWrite( File, ptr, size, 1 );
  213.  
  214.         ch = *String++;
  215.  
  216.         if( ch == '%' )
  217.         FPutC( File, '%' );
  218.         else {
  219.         struct Descriptor *d = Desc;
  220.  
  221.         while( d->Key )
  222.             if( ch == d->Key ) {
  223.             FPuts( File, d->Meaning );
  224.             break;
  225.             } else
  226.             d++;
  227.         }
  228.  
  229.         ptr  = String;
  230.         size = 0;
  231.  
  232.     } else {
  233.         size += 1;
  234.     }
  235.     }
  236.  
  237.     FWrite( File, ptr, size, 1 );
  238. }
  239. ///
  240. /// AddGadgetKind
  241. BOOL AddGadgetKind( __A0 struct Expander *Base, __A1 struct Node *Node )
  242. {
  243.     BOOL            ret = FALSE;
  244.     struct IEXNode *ex;
  245.  
  246.     if( ex = AllocMem( sizeof( struct IEXNode ), MEMF_CLEAR )) {
  247.     struct IEXNode *ex2, *ex3 = NULL;
  248.     BYTE            pri = Node->ln_Pri;
  249.  
  250.     ex->Base        = Base;
  251.     ex->Node.ln_Pri = pri;
  252.     ex->ID          = ExId;
  253.  
  254.     for( ex2 = IE.Expanders.mlh_Head; ex2->Node.ln_Succ; ex2 = ex2->Node.ln_Succ )
  255.         if( ex2->Base == Base ) {
  256.         ex->ID = ex2->ID;
  257.         break;
  258.         }
  259.  
  260.     if( ex->ID == ExId )
  261.         ExId += 1;
  262.  
  263.     ex2 = IE.Expanders.mlh_Head;
  264.     while(( ex2->Node.ln_Succ ) && ( ex2->Node.ln_Pri < pri )) {
  265.         ex3 = ex2;
  266.         ex2 = ex2->Node.ln_Succ;
  267.     }
  268.  
  269.     Insert(( struct List * )&IE.Expanders, ( struct Node * )ex, ( struct Node * )ex3 );
  270.  
  271.     memcpy( &ex->Copy, Node, sizeof( struct Node ));
  272.  
  273.     AddTail(( struct List * )&listgadgets, &ex->Copy );
  274.  
  275.     ret = TRUE;
  276.     }
  277.  
  278.     return( ret );
  279. }
  280. ///
  281. /// AddARexxCmd
  282. BOOL AddARexxCmd( __A0 struct ExCmdNode *Cmd )
  283. {
  284.     BOOL                ret = FALSE;
  285.     struct ExCmdNode   *Node;
  286.  
  287.     if( Node = AllocMem( sizeof( struct ExCmdNode ), 0L )) {
  288.  
  289.     CopyMem(( char * )Cmd, ( char * )Node, ( long )sizeof( struct ExCmdNode ));
  290.  
  291.     Node->Node.ln_Type = 1;
  292.     Node->ID           = FirstExId;
  293.  
  294.     AddTail(( struct List * )&RexxCommands, ( struct Node * )Node );
  295.     }
  296.  
  297.     return( ret );
  298. }
  299. ///
  300.  
  301.  
  302. //      Source related functions
  303.  
  304. /// IEXS_Globals
  305. void IEXS_Globals( __A0 struct GenFiles *Files )
  306. {
  307.     struct IEXNode  *ex;
  308.     struct Expander *IEXBase;
  309.  
  310.     for( ex = IE.Expanders.mlh_Head; ex->Node.ln_Succ; ex = ex->Node.ln_Succ ) {
  311.     if( ex->UseCount ) {
  312.         IEXBase = ex->Base;
  313.         IEX_WriteGlobals( ex->ID, Files, &IE );
  314.     }
  315.     }
  316. }
  317. ///
  318. /// IEXS_Setup
  319. void IEXS_Setup( __A0 struct GenFiles *Files )
  320. {
  321.     struct IEXNode  *ex;
  322.     struct Expander *IEXBase;
  323.  
  324.     for( ex = IE.Expanders.mlh_Head; ex->Node.ln_Succ; ex = ex->Node.ln_Succ ) {
  325.     if( ex->UseCount ) {
  326.         IEXBase = ex->Base;
  327.         IEX_WriteSetup( ex->ID, Files, &IE );
  328.     }
  329.     }
  330. }
  331. ///
  332. /// IEXS_CloseDown
  333. void IEXS_CloseDown( __A0 struct GenFiles *Files )
  334. {
  335.     struct IEXNode  *ex;
  336.     struct Expander *IEXBase;
  337.  
  338.     for( ex = IE.Expanders.mlh_Head; ex->Node.ln_Succ; ex = ex->Node.ln_Succ ) {
  339.     if( ex->UseCount ) {
  340.         IEXBase = ex->Base;
  341.         IEX_WriteCloseDown( ex->ID, Files, &IE );
  342.     }
  343.     }
  344. }
  345. ///
  346. /// IEXS_Headers
  347. void IEXS_Headers( __A0 struct GenFiles *Files )
  348. {
  349.     struct IEXNode  *ex;
  350.     struct Expander *IEXBase;
  351.  
  352.     for( ex = IE.Expanders.mlh_Head; ex->Node.ln_Succ; ex = ex->Node.ln_Succ ) {
  353.     if( ex->UseCount ) {
  354.         IEXBase = ex->Base;
  355.         IEX_WriteHeaders( ex->ID, Files, &IE );
  356.     }
  357.     }
  358. }
  359. ///
  360. /// IEXS_RenderMinusZero
  361. void IEXS_RenderMinusZero( __A0 struct GenFiles *Files )
  362. {
  363.     struct IEXNode  *ex;
  364.     struct Expander *IEXBase;
  365.  
  366.     for( ex = IE.Expanders.mlh_Head; ex->Node.ln_Succ && ex->Base->Node.ln_Pri < 0; ex = ex->Node.ln_Succ ) {
  367.     if( ex->UseCount ) {
  368.         IEXBase = ex->Base;
  369.         IEX_WriteRender( ex->ID, Files, &IE );
  370.     }
  371.     }
  372. }
  373. ///
  374. /// IEXS_RenderPlusZero
  375. void IEXS_RenderPlusZero( __A0 struct GenFiles *Files )
  376. {
  377.     struct IEXNode  *ex;
  378.     struct Expander *IEXBase;
  379.  
  380.     ex = IE.Expanders.mlh_Head;
  381.     while(( ex->Node.ln_Succ ) && ( ex->Base->Node.ln_Pri < 0 ))
  382.     ex = ex->Node.ln_Succ;
  383.  
  384.     if( ex->Node.ln_Succ ) {
  385.     for( ; ex->Node.ln_Succ; ex = ex->Node.ln_Succ ) {
  386.         if( ex->UseCount ) {
  387.         IEXBase = ex->Base;
  388.         IEX_WriteRender( ex->ID, Files, &IE );
  389.         }
  390.     }
  391.     }
  392. }
  393. ///
  394. /// IEXS_IDCMP
  395. ULONG IEXS_IDCMP( __D0 ULONG idcmp )
  396. {
  397.     struct IEXNode  *ex;
  398.     struct Expander *IEXBase;
  399.  
  400.     for( ex = IE.Expanders.mlh_Head; ex->Node.ln_Succ; ex = ex->Node.ln_Succ )
  401.     if( ex->UseCount ) {
  402.         IEXBase = ex->Base;
  403.         idcmp = IEX_GetIDCMP( ex->ID, idcmp, &IE );
  404.     }
  405.  
  406.     return( idcmp );
  407. }
  408. ///
  409. /// IEXS_Data
  410. void IEXS_Data( __A0 struct GenFiles *Files )
  411. {
  412.     struct IEXNode  *ex;
  413.     struct Expander *IEXBase;
  414.  
  415.     for( ex = IE.Expanders.mlh_Head; ex->Node.ln_Succ; ex = ex->Node.ln_Succ ) {
  416.     if( ex->UseCount ) {
  417.         IEXBase = ex->Base;
  418.         IEX_WriteData( ex->ID, Files, &IE );
  419.     }
  420.     }
  421. }
  422. ///
  423. /// IEXS_ChipData
  424. void IEXS_ChipData( __A0 struct GenFiles *Files )
  425. {
  426.     struct IEXNode  *ex;
  427.     struct Expander *IEXBase;
  428.  
  429.     for( ex = IE.Expanders.mlh_Head; ex->Node.ln_Succ; ex = ex->Node.ln_Succ ) {
  430.     if( ex->UseCount ) {
  431.         IEXBase = ex->Base;
  432.         IEX_WriteChipData( ex->ID, Files, &IE );
  433.     }
  434.     }
  435. }
  436. ///
  437. /// IEXS_Support
  438. void IEXS_Support( __A0 struct GenFiles *Files )
  439. {
  440.     struct IEXNode *ex;
  441.  
  442.     for( ex = IE.Expanders.mlh_Head; ex->Node.ln_Succ; ex = ex->Node.ln_Succ ) {
  443.     if( ex->UseCount ) {
  444.  
  445.         struct IEXNode *ex2;
  446.         BOOL            ok = TRUE;
  447.  
  448.         for( ex2 = IE.Expanders.mlh_Head; ex2 != ex; ex2 = ex2->Node.ln_Succ ) {
  449.         if(!( strcmp( ex->Support, ex2->Support )) && ( ex2->UseCount ))
  450.             ok = FALSE;
  451.         }
  452.  
  453.         if( ok )
  454.         FPuts( Files->Std, ex->Support );
  455.     }
  456.     }
  457. }
  458. ///
  459. /// IEXS_OpenWnd
  460. void IEXS_OpenWnd( __A0 struct GenFiles *Files )
  461. {
  462.     struct IEXNode  *ex;
  463.     struct Expander *IEXBase;
  464.  
  465.     for( ex = IE.Expanders.mlh_Head; ex->Node.ln_Succ; ex = ex->Node.ln_Succ ) {
  466.     if( ex->UseCount ) {
  467.         IEXBase = ex->Base;
  468.         IEX_WriteOpenWnd( ex->ID, Files, &IE );
  469.     }
  470.     }
  471. }
  472. ///
  473. /// IEXS_CloseWnd
  474. void IEXS_CloseWnd( __A0 struct GenFiles *Files )
  475. {
  476.     struct IEXNode  *ex;
  477.     struct Expander *IEXBase;
  478.  
  479.     for( ex = IE.Expanders.mlh_Head; ex->Node.ln_Succ; ex = ex->Node.ln_Succ ) {
  480.     if( ex->UseCount ) {
  481.         IEXBase = ex->Base;
  482.         IEX_WriteCloseWnd( ex->ID, Files, &IE );
  483.     }
  484.     }
  485. }
  486. ///
  487.  
  488. //      Objects management
  489. /// AddObject
  490. void AddObject( UWORD node )
  491. {
  492.     struct IEXNode     *ex;
  493.     UWORD               cnt, ID;
  494.     struct Node        *Node;
  495.  
  496.     Node = ( struct Node * )&listgadgets;
  497.  
  498.     for( cnt = 0; cnt <= node; cnt++ )
  499.     Node = Node->ln_Succ;
  500.  
  501.     ex = IE.Expanders.mlh_Head;
  502.     while( &ex->Copy != Node )
  503.     ex = ex->Node.ln_Succ;
  504.  
  505.     IEXBase = ex->Base;
  506.     ID      = ex->ID;
  507.  
  508.     if( IEXBase->Resizable || IEXBase->Movable ) {
  509.     WORD    x1, x2, y1, y2, swap;
  510.     UWORD   w, h;
  511.  
  512.     Stat( CatCompArray[ MSG_DRAW_GAD ].cca_Str, FALSE, 0 );
  513.  
  514.     ActivateWindow( IE.win_active );
  515.  
  516.     IE.flags &= ~RECTFIXED;
  517.  
  518.     if(!( IEXBase->Resizable ))
  519.         IE.flags |= RECTFIXED;
  520.  
  521.     DrawRect( IEXBase->Width, IEXBase->Height );
  522.  
  523.     offx = offy = 0;
  524.     Coord();
  525.  
  526.  
  527.     x1 = clickx;
  528.     x2 = lastx;
  529.     y1 = clicky;
  530.     y2 = lasty;
  531.  
  532.     if( x2 < x1 ) {
  533.         swap = x1;
  534.         x1 = x2;
  535.         x2 = swap;
  536.     }
  537.  
  538.     if( y2 < y1 ) {
  539.         swap = y1;
  540.         y1 = y2;
  541.         y2 = swap;
  542.     }
  543.  
  544.     if(!( IEXBase->Resizable )) {
  545.         w = IEXBase->Width;
  546.         h = IEXBase->Height;
  547.     } else {
  548.         w = x2 - x1 + 1;
  549.         h = y2 - y1 + 1;
  550.     }
  551.  
  552.     DisattivaTuttiGad();
  553.  
  554.     if( IEX_Add( ex->ID, &IE, x1, y1, w, h )) {
  555.  
  556.         ((struct BOOPSIInfo *)IE.win_info->wi_Gadgets.mlh_TailPred )->Node.ln_Type = IEXBase->Kind;
  557.  
  558.         switch( IEXBase->Kind ) {
  559.         case IEX_BOOPSI_KIND:
  560.             BoopsiEditor(( struct BOOPSIInfo *)IE.win_info->wi_Gadgets.mlh_TailPred );
  561.             break;
  562.  
  563.         default:
  564.             IEX_Edit( ex->ID, &IE );
  565.         }
  566.  
  567.         if(!( IE.win_info->wi_NumGads ))
  568.         MenuGadgetAttiva();
  569.  
  570.         RifaiGadgets();
  571.         RinfrescaFinestra();
  572.         IE.flags &= ~SALVATO;
  573.  
  574.         Stat( CatCompArray[ MSG_GAD_ADDED ].cca_Str, FALSE, 0 );
  575.  
  576.     } else
  577.         Stat( CatCompArray[ ERR_NOMEMORY ].cca_Str, TRUE, 0 );
  578.  
  579.     } else {
  580.     if(!( IEX_Add( ex->ID, &IE, 0, 0, IEXBase->Width, IEXBase->Height )))
  581.         Stat( CatCompArray[ ERR_NOMEMORY ].cca_Str, TRUE, 0 );
  582.     else {
  583.         IEX_Edit( ex->ID, &IE );
  584.  
  585.         if(!( IE.win_info->wi_NumGads ))
  586.         MenuGadgetAttiva();
  587.  
  588.         RifaiGadgets();
  589.         RinfrescaFinestra();
  590.         IE.flags &= ~SALVATO;
  591.  
  592.         Stat( CatCompArray[ MSG_GAD_ADDED ].cca_Str, FALSE, 0 );
  593.     }
  594.     }
  595. }
  596. ///
  597.  
  598.